/*
 * ResistanceTemperaturePlot.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   ResistanceTemperaturePlot.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   29 dec 2012
 */

#include <stdexcept>
#include <boost/shared_ptr.hpp>

#include "TemperatureScale.hpp"
#include "Thermistor.hpp"

#include "ResistanceTemperaturePlot.hpp"

using boost::shared_ptr;

namespace tltgen {

  ResistanceTemperaturePlot::ResistanceTemperaturePlot(const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& solver, const shared_ptr<TemperatureScale const>& temperatureScale) :
    TemperaturePlot(thermistorTable, solver, temperatureScale)
  {
    setXBoundsAuto(false);
    setYBoundsAuto(false);
  }

  std::string ResistanceTemperaturePlot::getTitle() const
  {
    return "Resistance - Temperature plot";
  }
  
  unsigned int ResistanceTemperaturePlot::getYMaxDigits() const
  {
    return 3;
  }

  std::string ResistanceTemperaturePlot::getYLabel() const
  {
    return "Resistance (k#(550))";
  }

  tuple<PLFLT, PLFLT> ResistanceTemperaturePlot::getYBoundsAuto() const
  {
    tuple<Resistance, Resistance> rBounds = this->thermistorTable->getResistanceBounds();
    return tuple<PLFLT, PLFLT>(rBounds.get<0>()/1000.0, rBounds.get<1>()/1000.0);
  }
  

  bool ResistanceTemperaturePlot::isValidRow(ThermistorTableRow* thermistorTableRow) const
  {
    return thermistorTableRow->hasValidTemperature() && thermistorTableRow->hasValidResistance();
  }
  PLFLT ResistanceTemperaturePlot::getYValue(ThermistorTableRow* thermistorTableRow) const
  {
    return thermistorTableRow->getResistance()/1000;
  }
  PLFLT ResistanceTemperaturePlot::getYValue(const shared_ptr<Thermistor>& thermistor, PLFLT xValue) const
  {
    return thermistor->getResistance(this->temperatureScale->toKelvin(xValue))/1000;
  }
}

